﻿using System;
using Microsoft.SharePoint.Administration;

namespace SKN
{
    /// <summary>
    /// Contains common method for Timer
    /// </summary>
    public static class Timer
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="webApplication"></param>
        /// <param name="sJobName"></param>
        /// <param name="contentDBGUID"></param>
        public static void ExecuteTimerJob(SPWebApplication webApplication, string sJobName, string contentDBGUID)
        {
            foreach (SPJobDefinition job in webApplication.JobDefinitions)
            {
                string jobTitle = job.Title;

                if (jobTitle == sJobName)
                {
                    job.Execute(new Guid(contentDBGUID));
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="webApplication"></param>
        /// <param name="sJobName"></param>
        public static void DeleteTimerJob(SPWebApplication webApplication, string sJobName)
        {
            // Delete the job.  
            foreach (SPJobDefinition job in webApplication.JobDefinitions)
            {
                if (string.Equals(job.Name, sJobName, StringComparison.InvariantCultureIgnoreCase))
                {
                    job.Delete();
                    break; //Assuming only one job exist
                }
            }
        }
    }
}